﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using System.Net.Http;
using System.Net.Http.Headers;

namespace ConsoleApplication2
{ 
    /// <summary>
    /// 网络提交方法
    /// </summary>
    public class HttpClientHelp
    {
        public static void sendMessage(string url, Object json)
        {
            String jsonString = JsonHelper.SerializeObject(json);
            sendMessage(url, jsonString);
        }

        public static void sendMessage(String url, String jsonString)
        {
            HttpClient client = new HttpClient();
            HttpContent httpContext = new StringContent(jsonString, Encoding.UTF8);
            httpContext.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            //await异步等待回应
            client.PostAsync(url, httpContext).ContinueWith(t =>
            {
                // Get HTTP response from completed task. 
                HttpResponseMessage response = t.Result;
                // Check that response was successful or throw exception 
                response.EnsureSuccessStatusCode();
                // Read response asynchronously as JsonValue and write out top facts for each country 
                response.Content.ReadAsStringAsync().ContinueWith(
                    (readTask) =>
                    {
                        Console.WriteLine(readTask.Result+" time:==>"+DateTime.Now);

                    });
            });


        }
    }
}
